<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>注册</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        .register-container {
            background-color: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            width: 300px;
        }

        .form-group {
            margin-bottom: 15px;
        }

        label {
            display: block;
            margin-bottom: 5px;
        }

        input[type="text"],
        input[type="password"],
        input[type="email"] {
            width: 100%;
            padding: 8px;
            box-sizing: border-box;
            border: 1px solid #ccc;
            border-radius: 4px;
        }

        button {
            width: 100%;
            padding: 10px;
            background-color: #007bff;
            color: #fff;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            margin-top: 10px;
        }

        button:hover {
            background-color: #0056b3;
        }

        .verification {
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .verification input {
            width: 60%;
        }

        .verification button {
            width: 35%;
        }

        .countdown {
            background-color: #6c757d;
            cursor: not-allowed;
        }
    </style>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<div class="register-container">
    <h2>用户注册</h2>
    <form id="register-form" action="/QQ/toRegister" method="post">
        <div class="form-group">
            <label for="name">用户名</label>
            <input type="text" id="name" name="name" required>
        </div>
        <div class="form-group">
            <label for="password">密码</label>
            <input type="password" id="password" name="password" required>
        </div>
        <div class="form-group">
            <label for="email">邮箱</label>
            <input type="email" id="email" name="email" required>
        </div>
        <div class="form-group verification">
            <input type="text" id="code" name="code" placeholder="验证码" required>
            <button type="button" id="get-code">获取验证码</button>
        </div>
        <button type="submit">注册</button>
    </form>
</div>

<script>
    $(document).ready(function() {
        $('#get-code').click(function() {
            var email = $('#email').val();
            if (email === '') {
                alert('请先输入邮箱');
                return;
            }

            // 禁用按钮，开始倒计时
            var $btn = $('#get-code');
            $btn.prop('disabled', true);
            var countdown = 60; // 倒计时的秒数
            var interval = setInterval(function() {
                if (countdown <= 0) {
                    clearInterval(interval);
                    $btn.prop('disabled', false).text('获取验证码');
                } else {
                    $btn.text(countdown + '秒后重试');
                }
                countdown--;
            }, 1000);

            // 调用后端发送验证码的接口
            $.ajax({
                type: 'POST',
                url: '/code/sendCode',
                data: { email: email },
                success: function(response) {
                    alert('验证码已发送，请检查你的邮箱。');
                },
                error: function() {
                    alert('发送验证码失败，请稍后重试。');
                    clearInterval(interval); // 如果发送失败，停止倒计时
                    $btn.prop('disabled', false).text('获取验证码');
                }
            });
        });
    });
</script>
</body>
</html>
